﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    public class ManagePendingStockRequest
    {
        /// <summary>
        ///Metoda priradi pozadavek na nabiti kreditu.
        ///</summary>
        public static void RequestCreditCharge(int UserID, int TeamID, int ChargeRequested)
        {
            AppLib.PendingStockRequest charge = new AppLib.PendingStockRequest();
            var db = MidApp.LinqUse.DBConn;

            charge.UserID = UserID;
            charge.TeamID = TeamID;
            charge.ChargeRequest = ChargeRequested;

            db.PendingStockRequests.InsertOnSubmit(charge);
            db.SubmitChanges();
        }
        /// <summary>
        ///Metoda vraci vsechny pozadavky uzviatele na dobiti
        ///</summary>
        public static List<AppLib.PendingStockRequest> GetPendingStockRequests(int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            var aux = (from Request in db.PendingStockRequests where Request.UserID == UserID select Request).ToList();
            
            return aux;
        }
        /// <summary>
        ///Metoda vraci pozadavky na dobiti pro dany tym
        ///</summary>
        public static List<AppLib.PendingStockRequest> GetPendingStockRequestsPerTeam(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            var aux = (from Request in db.PendingStockRequests where Request.TeamID == TeamID select Request).ToList();

            return aux;
        }
        /// <summary>
        ///Metoda odstrani pozadavek na dobiti z db
        ///</summary>
        public static void DeleteRequest(int ChargeID)
        {
            var db = MidApp.LinqUse.DBConn;

            AppLib.PendingStockRequest rq = (from Request in db.PendingStockRequests where Request.ChargeID==ChargeID select Request).First();
            db.PendingStockRequests.DeleteOnSubmit(rq);
            db.SubmitChanges();

        }
        /// <summary>
        ///Metoda odstrani pozadavky uzivatele na dobiti vuci tymu
        ///</summary>
        public static void DeleteRequests(int UserID, int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            List<AppLib.PendingStockRequest> rq = (from Request in db.PendingStockRequests where Request.UserID == UserID && Request.TeamID==TeamID select Request).ToList();
            try
            {

                foreach (var item in rq)
                {
                    db.PendingStockRequests.DeleteOnSubmit(item);
                    db.SubmitChanges();

                }
            }
            catch (Exception)
            {

            }

        }
        /// <summary>
        ///Metoda vrati pozadavek na dobiti z db
        ///</summary>
        public static AppLib.PendingStockRequest GetRequest(int ChargeID)
        {
            var db = MidApp.LinqUse.DBConn;

            var aux = (from Request in db.PendingStockRequests where Request.ChargeID == ChargeID select Request).First();

            return aux;
        }
    }
}
